﻿function CG_initGroup(id){
	var CG=new Object();
	CG.app=this;
	CG.groupId=id;
	CG.userList=new Array();
	CG.addUser=CG_addUser;
	CG.delUser=CG_delUser;
	CG.getUserListInfos=CG_getUserListInfos;
	CG.account="a";
	CG.vister="v";
	CG.chatLog="";
	CG.copId="";
	CG.STime="";
	CG.ETime="";
	CG.chatLogIndex=0;
	return CG;
}
//broadcase msg in group by users
function client_SendMsgToGroup(msg){
	
	var dtStr=GetTimeStr();
	
	this.info.CG.ETime=dtStr;
	if(this.info.CG.STime==""){
		this.info.CG.STime=dtStr;
	}
	
	var msgstr="<MsgItem>";
	msgstr+="<id>"+(++this.info.CG.chatLogIndex).toString()+"</id>";
	msgstr+="<datetime>"+dtStr+"</datetime>";
	msgstr +="<color>"+ msg.color+"</color>";
	msgstr +="<font>"+ msg.font+"</font>";
	if(msg.type=="m"){
		msgstr +="<msg><![CDATA["+msg.msgbody+"]]></msg>";
	}
	else if(msg.type=="l"){
		msgstr +="<msg><![CDATA[send link:"+msg.msgbody+"]]></msg>";
	}
	msgstr +="<size>"+ msg.size+"</size>";
	msgstr +="<sender>"+ msg.who+"</sender>";
	msgstr +="</MsgItem>";
	this.info.CG.chatLog+=msgstr;
	//trace(this+"++"+this.groupId+"::::"+msg.who+"|"+msg.color+"|"+msg.font+"|"+msg.size+"|"+msg.msgbody);
	CG_BroadcastMsgByGroup(this.info.CG.groupId,"ReceiveMsgFromGroup",msg);
}


function CG_addUser(userInfo){
	userInfo.client.SendMsgToGroup=client_SendMsgToGroup;
	this.copId=userInfo.copId;
	if(userInfo.client.clientType=="AC"){
		this.account=userInfo.nickname;
	}
	else if(userInfo.client.clientType=="VT"){
		this.vister=userInfo.nickname;
	}
	this.userList.push(userInfo);
	this.getUserListInfos();
	this.app.CG_BroadcastMsgByGroup(this.groupId,"updataGroupUserList",this.getUserListInfos());
}
function CG_delUser(client){
	for(var i=0;i<this.userList.length;i++){
		//trace(this.userList[i].username+"=="+client.info.username+"::"+(this.userList[i].username==client.info.username));
		if(this.userList[i].username==client.info.username){
			trace("del user:["+this.userList[i].username+"]");
			this.userList.splice(i,1);
			i--;
		}
	}
	if(this.userList.length==0){
		this.app.chatGroupCount--;
		var login_url = configs.chatLogServerUrl +"?action=chat";
		
		/*
		var loader = new LoadVars();
		loader.chatLog = escape("<MsgItems>" + this.chatLog + "</MsgItems>");
		loader.account=this.account;
		loader.vister=this.vister;
		loader.copId=this.copId;
		loader.app=this.app;
		loader.st=escape(this.STime);
		loader.et = escape(this.ETime);
		*/
		
		login_url += "&account="+escape(this.account);
		login_url += "&vister="+escape(this.vister);
		login_url += "&copId="+this.copId;
		login_url += "&st=" + escape(this.STime);
		login_url += "&et=" + escape(this.ETime);
		login_url += "&__r=" + Math.floor(Math.random() * 1000).toString();
		
		var msgxml = new XML("<MsgItems>" + this.chatLog + "</MsgItems>");
		var rsxml = new XML();
		rsxml.app = this.app;
		//this.app.writeLog("upload chat log:\r\n"+loader.chatLog);
		
		
		rsxml.onLoad = function(success) {
			if(!success){
				this.app.writeLog("Chat Record Faild!");
			}else{
				this.app.writeLog("Chat Recorded!");
			}
		}
		trace("* ChatLogRecord:"+login_url);
		msgxml.sendAndLoad(login_url, rsxml);
		/*
		loader.sendAndLoad(login_url,loader,"POST");
		loader.onLoad = function(success)
		{
			if(!success){
				this.app.writeLog("Chat Record Faild!");
			}else{
				this.app.writeLog("Chat Recorded!");
			}
		}
		*/
		delete this.app.chatGroup[this.groupId];
		trace("group count:"+this.app.chatGroupCount);
	}
	this.app.CG_BroadcastMsgByGroup(this.groupId,"updataGroupUserList",this.getUserListInfos());
}
function CG_BroadcastMsgByGroup(id,FunName,msg){ 
	var CG=chatGroup[id];
	if(CG!=undefined){
		for(var i=0;i<CG.userList.length;i++){
			var c=CG.userList[i].client;
			CallClient(c,FunName,msg);
		}
	}
}

function CG_getUserListInfos(){
	var ar=[];
	for(var i=0;i<this.userList.length;i++){
		ar.push({
			username:this.userList[i].username,
			nickname:this.userList[i].nickname
		})
	}
	//MyTrace(ar);
	return ar;
}